package com.ruoyi.web.controller.GoodsLx;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.GoodsLx.domain.GoodsLx;
import com.ruoyi.GoodsLx.service.IGoodsLxService;
import com.ruoyi.Planting.domain.Planting;
import com.ruoyi.School.domain.School;
import com.ruoyi.School.service.ISchoolService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateTools;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.QiniuCloudUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;

/**
 * 商品类型 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-07-22
 */
@Controller
@RequestMapping("/GoodsLx/goodsLxStore")
public class GoodsLxStoreController extends BaseController
{
    private String prefix = "GoodsLx/goodsLxStore";
	
	@Autowired
	private IGoodsLxService goodsLxService;
	
	@Autowired
	private ISchoolService schoolService;
	
	@RequiresPermissions("GoodsLx:goodsLx:view")
	@GetMapping()
	public String goodsLx()
	{
	    return prefix + "/goodsLx";
	}
	
	/**
	 * 查询商品类型列表
	 */
	@RequiresPermissions("GoodsLx:goodsLx:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(GoodsLx goodsLx)
	{
		goodsLx.setSchoolId(getSchoolId());
		goodsLx.setGoodsLxStore(1);
		startPage();
        List<GoodsLx> list = goodsLxService.selectGoodsLxList(goodsLx);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出商品类型列表
	 */
	@RequiresPermissions("GoodsLx:goodsLx:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsLx goodsLx)
    {
    	List<GoodsLx> list = goodsLxService.selectGoodsLxList(goodsLx);
        ExcelUtil<GoodsLx> util = new ExcelUtil<GoodsLx>(GoodsLx.class);
        return util.exportExcel(list, "goodsLx");
    }
	
	/**
	 * 新增商品类型
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存商品类型
	 */
	@RequiresPermissions("GoodsLx:goodsLx:add")
	@Log(title = "商品类型", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(GoodsLx goodsLx)
	{		
		String imgData = goodsLx.getImgs();
		if ((!StringUtils.isEmpty(imgData)) && (!imgData.startsWith("http"))) {
			imgData = imgData.substring(imgData.indexOf(",") + 1);						
			byte[] bytes=Base64Utils.decodeFromString(imgData);
			String imageName = UUID.randomUUID().toString();
			QiniuCloudUtil qiniuUtil = new QiniuCloudUtil();
			try {
				String url = qiniuUtil.put64image(bytes, imageName);
				goodsLx.setImgs(url);
			} catch (Exception e) {
				e.printStackTrace();
			}			
		}
		goodsLx.setSchoolId(getSchoolId());
		goodsLx.setCreactDate(DateTools.getYMDhms());
		goodsLx.setValid(1);
		goodsLx.setCreateBy(ShiroUtils.getSysUser().getUserName());
		goodsLx.setGoodsLxStore(1);
		return toAjax(goodsLxService.insertGoodsLx(goodsLx));
	}

	/**
	 * 修改商品类型
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		GoodsLx goodsLx = goodsLxService.selectGoodsLxById(id);
		mmap.put("goodsLx", goodsLx);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存商品类型
	 * @throws IOException 
	 */
	@RequiresPermissions("GoodsLx:goodsLx:edit")
	@Log(title = "商品类型", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(GoodsLx goodsLx) throws IOException
	{		
		String imgData = goodsLx.getImgs();
		if ((!StringUtils.isEmpty(imgData)) && (!imgData.startsWith("http"))) {
			int id=goodsLx.getId();
			GoodsLx persist=goodsLxService.selectGoodsLxById(id);
			if(!StringUtils.isEmpty(persist.getImgs())){
				QiniuCloudUtil qiniuUtilforDelete = new QiniuCloudUtil();
				qiniuUtilforDelete.delete(persist.getImgs());
			}
			imgData = imgData.substring(imgData.indexOf(",") + 1);						
			byte[] bytes=Base64Utils.decodeFromString(imgData);
			String imageName = UUID.randomUUID().toString();
			QiniuCloudUtil qiniuUtil = new QiniuCloudUtil();
			try {
				String url = qiniuUtil.put64image(bytes, imageName);
				goodsLx.setImgs(url);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		goodsLx.setModifyDate(DateTools.getYMDhms());	
		goodsLx.setModifyBy(ShiroUtils.getSysUser().getUserName());
		return toAjax(goodsLxService.updateGoodsLx(goodsLx));
	}
	
	/**
	 * 删除商品类型
	 */
	@RequiresPermissions("GoodsLx:goodsLx:remove")
	@Log(title = "商品类型", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(goodsLxService.deleteGoodsLxByIds(ids));
	}
	
	public String getSchoolId() {
		String deptName = ShiroUtils.getSysUser().getDept().getDeptName();
		School example = new School();
		example.setSchoolName(deptName);
		List<School> persistList = schoolService.selectSchoolList(example);
		if (persistList.size() > 0) {
			School persist = persistList.get(0);
			return persist.getId().toString();
		} else {
			return StringUtils.EMPTY;
		}
	}
	
}
